//
//  GQPicturePlayerViewController.m
//  TingMusic
//
//  Created by sunny on 16/4/20.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "GQPicturePlayerViewController.h"
#import "GQPicturePlayerCell.h"
#import "GQPicturePlayerItem.h"
#import <WebKit/WebKit.h>
@interface GQPicturePlayerViewController ()
/** 模型数组 */
@property (nonatomic,strong)NSArray *itemArray;
/** 网页跳转的控制器 */
@property (nonatomic, strong)UIViewController *webPushViewConroller;

@end

@implementation GQPicturePlayerViewController

static NSString * const reuseIdentifier = @"Cell";

#pragma mark - 懒加载模型数组
+ (instancetype)collectionViewVController
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(GQUIScreenW, GQPicturePlayerHeight);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    GQPicturePlayerViewController *collectionVc = [[GQPicturePlayerViewController alloc] initWithCollectionViewLayout:layout];
    return collectionVc;
}

- (NSArray *)itemArray
{
    if (_itemArray == nil) {
        _itemArray = [NSArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    // Register cell classes
    [self.collectionView registerClass:[GQPicturePlayerCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
//    发送数据
    [self loadData];
    
//   监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageViewTap) name:@"imageViewDidTapotification" object:nil];
    
}


#pragma mark - 实现监听通知的方法
- (void)imageViewTap
{
    GQPicturePlayerCell *cell = (GQPicturePlayerCell *)[[self.collectionView visibleCells] firstObject];
    //    点击图片跳转到对应的web界面
    UIViewController *vc = [[UIViewController alloc]init];
    self.webPushViewConroller = vc;
    UIButton *leftButton = [[UIButton alloc]init];
    [leftButton setTitle:cell.item.randpic_desc forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"icon_king_periods_left_normal"] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    leftButton.font = [UIFont systemFontOfSize:15];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    leftButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [leftButton sizeToFit];
    [leftButton addTarget:self action:@selector(navBackButtonClick) forControlEvents:UIControlEventTouchUpInside];
    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    WKWebView *webView = [[WKWebView alloc]initWithFrame:vc.view.bounds];
    NSURL *url = [NSURL URLWithString:cell.item.code];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    [vc.view addSubview:webView];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    [self presentViewController:nav animated:YES completion:nil];
    

}

- (void)navBackButtonClick {
    [self.webPushViewConroller dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)loadData
{
    [GQHttpRequest GET:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.7.2.0&channel=xiaomi&operator=2&method=baidu.ting.plaza.index&cuid=E6B77D59F4F7B2824CC57E6E5CADD436" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.itemArray = [GQPicturePlayerItem mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"focus"][@"result"]];
        
//        刷新数据
        [self.collectionView reloadData];
        
//        开启定时器
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }] ;
 
}

static int i = 0;
#pragma mark - 定时器事件
- (void)timeChange
{
    if (i > self.itemArray.count-2) {
        i = -1;
    }
    i++;
    [self.collectionView setContentOffset:CGPointMake(i * self.collectionView.gq_width, 0) animated:YES];
    
}


#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GQPicturePlayerCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    GQPicturePlayerItem *item = self.itemArray[indexPath.row];
    cell.item = item;
    
    return cell;
}

@end
